import Guid from './guid.js';

var lang = {};
var PdfAnnotationController = (function ()
    {
        function pdfAnnotationController(options)
        {
            this.annotationTypeToCreate = 0;//不新建，1~7，详见文档定义。
            this.pageToCreate = -1;//不新建，1~7，详见文档定义。
            this.mouseDownElement = null;
            this.mouseDownPoint = null;
            this.mouseUpElement = null;
            this.mouseUpPoint = null;
            this.color = null;
            this.drawToolColor = "#FF0000";
            this.displayAnnotation = true;
            this.annotationbar = options.annotationbar || null;
            this.annotationList = null;
            this.previewData = options.previewData || {};

            var self = this;

            $(document).bind("OnDisplayAnnotationOpened", function (e)
            {
                self.loadAnnotationOnEveryPage();
                return false;
            });
            $(document).bind("OnDisplayAnnotationClosed", function (e)
            {
                self.removeAnnotationOnEveryPage();
                return false;
            });

            $(document).bind("OnRequestToDeleteAnnotation", function (e, sectionObject, func)
            {
                self.showMessageDialog(self.getDisplayText("msgDeleteAnnotationConfirm") /*确定要删除这条批注吗？*/, function ()
                {
                    self.deleteAnnotation(sectionObject.id, function ()
                    {
                        if (func)
                        {
                            func();
                        }
                    });
                }, true);
                return false;
            });
        }

        pdfAnnotationController.prototype = {
            setAnnotationBar: function (annotationbar)
            {
                this.annotationbar = annotationbar;
            },

            setColor: function (color)
            {
                this.color = color;
                $(document).trigger("OnColorSelected", color);
                if (this.creating)
                {
                    if (this.creating.type === 4)
                    {
                        if (this.creating.contents.shapes.length > 0)
                        {
                            this.creating.contents.shapes[0].color = color;
                            this.setAnnotationStyle(this.creating);
                        }
                        var pageView = window.PDFViewerApplication.pdfViewer.getPageView(this.creating.pageIndex);
                        var textareaQuery = $(pageView.drawLayerDiv).children("textarea[data-for=\"" + this.creating.id + "\"]");
                        textareaQuery.css("color", color);
                    }
                } else
                {
                    if (this.annotationTypeToCreate === 5)
                    {
                        this.drawToolColor = color;
                    } else if (this.annotationTypeToCreate === 6)
                    {
                        this.drawToolColor = color;
                    } else if (this.annotationTypeToCreate === 7)
                    {
                        this.drawToolColor = color;
                    } else if (this.annotationTypeToCreate === 8)
                    {
                        this.drawToolColor = color;
                    } else if (this.annotationTypeToCreate === 9)
                    {
                        this.drawToolColor = color;
                    }
                }
            },

            calculateHighLightSection: function (e)
            {
                var s = document.getSelection();
                var a = s.anchorNode;
                if (!(a instanceof Text))
                {
                    //这一定是选错了
                    this.resetSelection();
                    return;
                }
                var al = a.length;
                var ap = s.anchorOffset;
                var f = s.focusNode;
                var isSelectEndOfContent = false;//判断用户选择了文字后面的空格
                var f_previous = null;
                var f_previous_dom = null;
                //将JS文本对象转换为DOM对象
                var parseToDOM = function (str)
                {
                    var div = document.createElement("span");
                    if (typeof str == "string")
                        div.innerHTML = str;
                    return div.childNodes;
                }
                if (!(f instanceof Text))
                {
                    //这一定是选错了或者选中了文字后面的空格
                    //这时判断前一个元素是否是文本
                    f_previous = this.mouseUpElement.previousSibling.innerText;
                    if (typeof (f_previous) != "undefined" && f_previous != "")
                    {
                        f_previous_dom = parseToDOM(f_previous)[0];
                    }
                    if (!(f_previous_dom instanceof Text))
                    {
                        this.resetSelection();
                        return;
                    } else
                    {
                        isSelectEndOfContent = true;
                        f = f_previous_dom;
                        f.length = f_previous.length;
                        f.focusOffset = f_previous.length;
                    }
                }
                var fl = f.length;
                var fp = s.focusOffset;
                if (isSelectEndOfContent)
                {
                    fl = f.length;
                    fp = f.length;
                }
                if (a === f && ap === fp)
                {
                    //啥都没选
                    this.resetSelection();
                    return;
                }

                //我们要知道用户这一下子到底选中了哪些div
                //有可能用户是从上往下选择的，
                var errorTR = 30;//选择高速容错值
                var arrayObj = new Array();
                var iterator = this.mouseDownElement;
                arrayObj.push(iterator);
                var isBottomToTop = false;
                while (iterator !== this.mouseUpElement)
                {
                    iterator = $(iterator).next()[0];
                    if (typeof (iterator) == "undefined")
                    {
                        //这个神奇的用户竟然是从下往上选择的
                        isBottomToTop = true;
                        break;
                    }
                    arrayObj.push(iterator);
                };
                if (isSelectEndOfContent)
                {
                    if ((arrayObj.length) >= 2 && (arrayObj[arrayObj.length - 2] === this.mouseUpElement.previousSibling))
                    {
                        arrayObj.pop();
                    }
                }
                if (isBottomToTop)
                {
                    arrayObj.splice(0, arrayObj.length);
                    iterator = this.mouseUpElement;
                    arrayObj.push(iterator);
                    while (iterator !== this.mouseDownElement)
                    {
                        iterator = $(iterator).next()[0];
                        if (typeof (iterator) == "undefined")
                        {
                            //按道理这里是不应该出现的。
                            //到底是从上往下，还是从下往上选择的呢？我不管了。
                            this.resetSelection();
                            return;
                        }
                        arrayObj.push(iterator);
                    };
                }
                //如果选中的地方比鼠标选择的地方还远(容错值由var errorTR定义)，取消多余选中的部分
                //从上往下比较最后一个元素
                if (!isBottomToTop && ($(arrayObj[arrayObj.length - 1]).offset().top - e.pageY > errorTR))
                {
                    this.resetSelection();
                    return;
                }
                //从下往上比较第一个元素
                if (isBottomToTop && ((e.pageY - $(arrayObj[0]).offset().top) > errorTR))
                {
                    this.resetSelection();
                    return;
                }
                var getScaleX = function (transform)
                {
                    var divTransformArray = transform.split(" ");
                    var divTr = null;
                    for (var i = 0; i < divTransformArray.length; i++)
                    {
                        if (divTr != null && !isNaN(divTr))
                        {
                            break;
                        }
                        divTr = parseFloat(divTransformArray[i].replace("scaleX(", "").replace(")", ""));
                    }
                    return divTr;
                };
                var getRotate = function (transform)
                {
                    var divTransformArray = transform.split(" ");
                    var divTr = null;
                    for (var i = 0; i < divTransformArray.length; i++)
                    {
                        if (divTr != null && !isNaN(divTr))
                        {
                            break;
                        }
                        divTr = parseFloat(divTransformArray[i].replace("rotate(", "").replace("deg)", ""));
                    }
                    if (isNaN(divTr))
                    {
                        divTr = 0;
                    }
                    return divTr;
                };
                //好了，到这里为止，我们处理掉了各种错误，得到了最终选中的div数组。
                //接着我们要知道这些div到底占用了哪些地方，要得到最左边的div边界：
                //选中开始div从何处开始
                var startOfFirstDiv = parseFloat(arrayObj[0].style.left.replace(/px/gi, ""));
                //因为div被添加了缩放，所以需要算出它的真实显示宽度
                var firstDivTr = getScaleX(arrayObj[0].style.transform);
                if (isNaN(firstDivTr))
                {
                    firstDivTr = 1;
                }
                var firstDivOw = arrayObj[0].offsetWidth;
                if (isBottomToTop)
                {
                    startOfFirstDiv += firstDivTr * firstDivOw * fp / fl;
                } else
                {
                    if (arrayObj.length === 1)
                    {
                        //当用户选中的内容就在一个div里面的时候，我不知道它是从左向右，还是从右向左选的，这个判断将选择较小的那个位置
                        startOfFirstDiv += firstDivTr * firstDivOw * Math.min(ap, fp) / al;
                    } else
                    {
                        startOfFirstDiv += firstDivTr * firstDivOw * ap / al;
                    }
                }
                //选中结束div在何处结束
                var endOfLastDiv = parseFloat(arrayObj[arrayObj.length - 1].style.left.replace(/px/gi, ""));
                var lastDivTr = getScaleX(arrayObj[arrayObj.length - 1].style.transform);
                if (isNaN(lastDivTr))
                {
                    lastDivTr = 1;
                }
                var lastDivOw = arrayObj[arrayObj.length - 1].offsetWidth;
                if (isBottomToTop)
                {
                    endOfLastDiv += lastDivTr * lastDivOw * ap / al;
                } else
                {
                    if (arrayObj.length === 1)
                    {
                        endOfLastDiv += lastDivTr * lastDivOw * Math.max(ap, fp) / fl;
                    } else
                    {
                        endOfLastDiv += lastDivTr * lastDivOw * fp / fl;
                    }
                }
                //到这里为止，跟选区document.getSelection()有关的东西就用不上了，除了它选中的那些文字：s.toString()
                //接下去计算section的位置，这是用于将来鼠标点击的位置，一点就弹出评论框
                var mostLeft = startOfFirstDiv;
                var mostRight = endOfLastDiv;
                var mostTop = parseFloat(arrayObj[0].style.top.replace(/px/gi, ""));
                var mostBottom = parseFloat(arrayObj[arrayObj.length - 1].style.top.replace(/px/gi, "")) + arrayObj[arrayObj.length - 1].offsetHeight;
                if (arrayObj.length === 1)
                {
                } else
                {
                    for (var i = 1; i < arrayObj.length; i++)
                    {
                        var div1 = arrayObj[i];
                        var l = parseFloat(div1.style.left.replace(/px/gi, ""));
                        mostLeft = Math.min(mostLeft, l);
                    }
                    for (var j = 0; j < arrayObj.length - 1; j++)
                    {
                        var div2 = arrayObj[j];
                        var div2Tr = getScaleX(div2.style.transform);
                        if (isNaN(div2Tr))
                        {
                            div2Tr = 1;
                        }
                        var div2Ow = div2.offsetWidth;
                        var r = parseFloat(div2.style.left.replace(/px/gi, "")) + div2Tr * div2Ow;
                        mostRight = Math.max(mostRight, r);
                    }
                }
                var pageView = window.PDFViewerApplication.pdfViewer.getPageView(this.pageToCreate);
                var textLayerDiv = pageView.textLayer.textLayerDiv;
                var tw = parseFloat(textLayerDiv.style.width.replace(/px/gi, ""));
                var th = parseFloat(textLayerDiv.style.height.replace(/px/gi, ""));
                var annotationLayer = pageView.annotationLayer.div;

                var sectionLeft = mostLeft / tw;
                var sectionWidth = (mostRight - mostLeft) / tw;
                var sectionTop = mostTop / th;
                var sectionHeight = (mostBottom - mostTop) / th;

                var sectionObject = new Object();
                sectionObject.pageIndex = this.pageToCreate;
                sectionObject.left = sectionLeft;
                sectionObject.top = sectionTop;
                sectionObject.width = sectionWidth;
                sectionObject.height = sectionHeight;
                sectionObject.editable = true;
                sectionObject.type = this.annotationTypeToCreate;
                sectionObject.contents = new Object();
                sectionObject.contents.text = s.toString();
                sectionObject.contents.shapes = new Array();
                var self = this;

                var firstDivLeft = parseFloat(arrayObj[0].style.left.replace(/px/gi, ""));
                var fRotate = getRotate(arrayObj[0].style.transform) * Math.PI / 180;
                var fsin = Math.sin(fRotate);
                var fcos = Math.cos(fRotate);
                if (arrayObj.length === 1)
                {
                    var divObject = new Object();
                    divObject.color = self.color;
                    divObject.left = (firstDivLeft - startOfFirstDiv) * (1 - fcos) / (mostRight - mostLeft);
                    divObject.top = (startOfFirstDiv - firstDivLeft) / (mostBottom - mostTop) * fsin;
                    divObject.width = 1;
                    divObject.height = 1;
                    divObject.sin = fsin;
                    divObject.cos = fcos;
                    divObject.type = sectionObject.type;
                    sectionObject.contents.shapes.push(divObject);
                } else
                {
                    var fLeft = (startOfFirstDiv - mostLeft - (startOfFirstDiv - firstDivLeft) * (1 - fcos)) / (mostRight - mostLeft);
                    var fTop = (startOfFirstDiv - firstDivLeft) / (mostBottom - mostTop) * fsin;
                    var fWidth = (firstDivLeft + firstDivTr * firstDivOw - startOfFirstDiv) / (mostRight - mostLeft);
                    var fHeight = arrayObj[0].offsetHeight / (mostBottom - mostTop);
                    if (fHeight < 0)
                    {
                        //出现这个情况，是因为段落文字发生了旋转。目前不支持，所以拒绝创建。
                        this.resetSelection();
                        return;
                    }
                    var divfObject = new Object();
                    divfObject.color = self.color;
                    divfObject.left = fLeft;
                    divfObject.top = fTop;
                    divfObject.width = fWidth;
                    divfObject.height = fHeight;
                    divfObject.type = sectionObject.type;
                    divfObject.sin = fsin;
                    divfObject.cos = fcos;
                    sectionObject.contents.shapes.push(divfObject);

                    for (var k = 1; k < arrayObj.length - 1; k++)
                    {
                        var d = arrayObj[k];
                        var dRotate = getRotate(d.style.transform) * Math.PI / 180;
                        var dsin = Math.sin(dRotate);
                        var dcos = Math.cos(dRotate);
                        var dL = parseFloat(d.style.left.replace(/px/gi, ""));
                        var dT = parseFloat(d.style.top.replace(/px/gi, ""));
                        var dTr = getScaleX(d.style.transform);
                        if (isNaN(dTr))
                        {
                            dTr = 1;
                        }
                        var dLeft = (dL - mostLeft) / (mostRight - mostLeft);
                        var dTop = (dT - mostTop) / (mostBottom - mostTop);
                        var dWidth = dTr * d.offsetWidth / (mostRight - mostLeft);
                        var dHeight = d.offsetHeight / (mostBottom - mostTop);
                        if (dHeight < 0)
                        {
                            //出现这个情况，是因为段落文字发生了旋转。目前不支持，所以拒绝创建。
                            this.resetSelection();
                            return;
                        }

                        var divmObject = new Object();
                        divmObject.color = self.color;
                        divmObject.left = dLeft;
                        divmObject.top = dTop;
                        divmObject.width = dWidth;
                        divmObject.height = dHeight;
                        divmObject.sin = dsin;
                        divmObject.cos = dcos;
                        divmObject.type = sectionObject.type;
                        sectionObject.contents.shapes.push(divmObject);
                    }
                    var lRotate = getRotate(arrayObj[arrayObj.length - 1].style.transform) * Math.PI / 180;
                    var lsin = Math.sin(lRotate);
                    var lcos = Math.cos(lRotate);
                    var lL = parseFloat(arrayObj[arrayObj.length - 1].style.left.replace(/px/gi, ""));
                    var lT = parseFloat(arrayObj[arrayObj.length - 1].style.top.replace(/px/gi, ""));
                    var lLeft = (lL - mostLeft) / (mostRight - mostLeft);
                    var lTop = (lT - mostTop) / (mostBottom - mostTop);
                    var lWidth = (endOfLastDiv - lL) / (mostRight - mostLeft);
                    var lHeight = arrayObj[arrayObj.length - 1].offsetHeight / (mostBottom - mostTop);
                    if (lHeight < 0)
                    {
                        //出现这个情况，是因为段落文字发生了旋转。目前不支持，所以拒绝创建。
                        this.resetSelection();
                        return;
                    }

                    var divlObject = new Object();
                    divlObject.color = self.color;
                    divlObject.left = lLeft;
                    divlObject.top = lTop;
                    divlObject.width = lWidth;
                    divlObject.height = lHeight;
                    divlObject.sin = lsin;
                    divlObject.cos = lcos;
                    divlObject.type = sectionObject.type;
                    sectionObject.contents.shapes.push(divlObject);
                }
                var gs = this.createAnnotation(annotationLayer);
                sectionObject.id = gs;
                this.setAnnotationStyle(sectionObject);
                this.resetSelection();
                this.saveAnnotation(sectionObject);
            },

            clearSelection: function ()
            {
                //为了兼容各种浏览器而写的奇怪的方法，只为取消选择
                if ("getSelection" in window)
                {
                    try
                    {
                        window.getSelection().removeAllRanges();
                    } catch (e)
                    {
                        try
                        {
                            window.getSelection().empty();
                        } catch (e)
                        {
                        }
                    }
                }
                else
                {
                    try
                    {
                        document.selection.empty();
                    } catch (e)
                    {
                    }
                }
            },
            resetSelection: function ()
            {
                var self = this;
                this.pageToCreate = -1;
                this.mouseDownElement = null;
                this.mouseDownPoint = null;
                this.mouseUpElement = null;
                this.mouseUpPoint = null;
                self.clearSelection();
            },

            resetDrawing: function ()
            {
                this.pageToCreate = -1;
                this.mouseDownPoint = null;
                this.mouseUpPoint = null;
                if (this.creating != null)
                {
                    $("#" + this.creating.id).remove();
                    this.creating = null;
                }
            },

            fixUnFinishedTextNote: function (pageIndex)
            {
                //加文本框这个事儿有点麻烦，因为从创建到完成，中间时间特别长，用户很有可能做一些非常奇怪的事情，导致中断了本来做的事情。
                //所以要恢复一下：
                if (this.pageToCreate === pageIndex && this.annotationTypeToCreate === 4 && this.creating != null)
                {
                    this.resetDrawing();
                }
            },

            prepareForHighlight: function (pageNumber)
            {
                var self = this;
                var pageIndex = pageNumber - 1;
                var pageView = window.PDFViewerApplication.pdfViewer.getPageView(pageIndex);
                var annotationLayer = pageView.annotationLayer.div;
                $("#viewer > div.page[data-page-number='" + pageNumber + "'] > div[class=\"textLayer\"] > span").mousedown(function (e)
                {
                    if (self.annotationbar == null || !self.annotationbar.opened)
                    {
                        return;
                    }
                    self.pageToCreate = pageIndex;
                    switch (self.annotationTypeToCreate)
                    {
                        case 1:
                        case 2:
                        case 3:
                            self.mouseDownElement = this;
                            break;
                    }
                    $(annotationLayer).children(".edoc2annotation").removeClass("clickable").addClass("unclickable");
                });
                $("#viewer > div.page[data-page-number='" + pageNumber + "'] > div[class=\"textLayer\"] > span").mouseup(function (e)
                {
                    if (self.annotationbar == null || !self.annotationbar.opened)
                    {
                        return;
                    }
                    if (pageIndex !== self.pageToCreate)
                    {
                        //我们不支持跨页的创建批注，即使是选择文本也不行。
                        self.resetSelection();
                        $(annotationLayer).children(".edoc2annotation").removeClass("unclickable").addClass("clickable");
                        return;
                    }
                    switch (self.annotationTypeToCreate)
                    {
                        case 1:
                            self.mouseUpElement = this;
                            self.calculateHighLightSection(e);
                            break;
                        case 2:
                            self.mouseUpElement = this;
                            self.calculateHighLightSection(e);
                            break;
                        case 3:
                            self.mouseUpElement = this;
                            self.calculateHighLightSection(e);
                            break;
                    }
                    $(annotationLayer).children(".edoc2annotation").removeClass("unclickable").addClass("clickable");
                });
            },

            prepareForDrawingOnEveryPage: function ()
            {
                for (var i = 0; i < window.PDFViewerApplication.pdfViewer.pagesCount; i++)
                {
                    var pageView = window.PDFViewerApplication.pdfViewer.getPageView(i);
                    if (pageView.loadingIconDiv == null)
                    {
                        this.prepareForDrawing(i + 1);
                    }
                }
            },

            prepareForDrawing: function (pageNumber)
            {
                var self = this;
                if (self.annotationbar == null || !self.annotationbar.opened)
                {
                    return;
                }
                var pageIndex = pageNumber - 1;
                var pageView = window.PDFViewerApplication.pdfViewer.getPageView(pageIndex);
                var annotationLayer = pageView.annotationLayer.div;
                switch (self.annotationTypeToCreate)
                {
                    case 4:
                        //这里是创建文本框的代码
                        var titleHeight = 25;
                        var bottomHeight = 35;
                        var resizerMarginRight = 1;
                        var paddingLeft = 5;
                        $(annotationLayer).css("pointer-events", "auto");
                        if ($("#viewer > div.page[data-page-number='" + pageNumber + "'] > div[class=\"drawLayer\"]").length !== 0)
                        {
                            return;
                        }
                        var pageContainer = $("#viewer > div.page[data-page-number='" + pageNumber + "']");
                        pageContainer.append("<div class=\"drawLayer\"></div>");
                        pageView.drawLayerDiv = pageContainer.children("div[class=\"drawLayer\"]")[0];
                        $(pageView.drawLayerDiv).css("display", "none");
                        $(annotationLayer).mousedown(function (e)
                        {
                            var obj = e.srcElement ? e.srcElement : e.target;
                            if (annotationLayer !== obj)
                            {
                                if ($(obj).hasClass("selectable"))
                                {
                                    return true;
                                }
                                return false;
                            }
                            if (pageContainer[0].offsetWidth - e.offsetX < 100)
                            {
                                //放在太右边了，写不下的。
                                return false;
                            }
                            if (self.creating == null)
                            {
                                self.pageToCreate = pageIndex;
                                self.mouseDownPoint = e;
                            }
                            return false;
                        });
                        $(annotationLayer).mouseup(function (e)
                        {
                            var obj = e.srcElement ? e.srcElement : e.target;
                            if (annotationLayer !== obj)
                            {
                                return false;
                            }
                            if (self.creating != null)
                            {
                                //我们把上一次鼠标放开之后创建的section存入window.PDFViewerApplication.annotationController.creating。
                                //鼠标放开之后，如果window.PDFViewerApplication.annotationController.creating为空，那就可以创建一个，否则上一次的创建的还没有处理完。
                                return false;
                            }
                            if (pageIndex !== self.pageToCreate)
                            {
                                //我们不支持跨页的创建批注。
                                self.resetDrawing();
                                return false;
                            }
                            if (Math.abs(self.mouseDownPoint.offsetX - e.offsetX) >= 1 || Math.abs(self.mouseDownPoint.offsetY - e.offsetY) >= 1)
                            {
                                //这人在创建的时候手抖了。我觉得这人可能并不是真的要创建。
                                self.resetDrawing();
                                return false;
                            }
                            $(pageView.drawLayerDiv).css("display", "block");
                            self.mouseUpPoint = e;
                            var sectionLeft = e.offsetX / pageView.drawLayerDiv.offsetWidth;
                            var sectionTop = e.offsetY / pageView.drawLayerDiv.offsetHeight;
                            var gs = self.createAnnotation(pageView.annotationLayer.div);
                            var sectionObject = new Object();
                            sectionObject.id = gs;
                            sectionObject.pageIndex = self.pageToCreate;
                            sectionObject.left = sectionLeft;
                            sectionObject.top = sectionTop;
                            sectionObject.editable = true;
                            sectionObject.type = self.annotationTypeToCreate;
                            sectionObject.contents = new Object();
                            sectionObject.contents.shapes = new Array();

                            self.creating = sectionObject;
                            var divString = "<div data-for=\"" + gs + "\" style=\"left:" + (e.offsetX - paddingLeft) + "px;top:" + (e.offsetY - titleHeight) + "px;position:absolute;opacity:1;width:" + (pageView.drawLayerDiv.offsetWidth - e.offsetX - 10 + paddingLeft) + "px;height:" + (titleHeight + 30 + 20 + bottomHeight) + "px\" class=\"textNoteTitle\">";
                            var topMoverString = "<div data-for=\"" + gs + "\" class=\"mouseOperationHint mouseOperationHintRectangle\" style=\"left:50%;top:12px\"></div>";
                            var buttonString = "<button data-for=\"" + gs + "\" class=\"form-button active textNoteFinishButton\">" + self.getDisplayText("ok") + "</button>";
                            var cancelButtonString = "<button data-for=\"" + gs + "\" class=\"textNoteCancelButton\" title=\"" + self.getDisplayText("cancel") + "\"></button>";
                            var textareaString = "<textarea data-for=\"" + gs + "\" style=\"left:" + e.offsetX + "px;top:" + e.offsetY + "px;width:" + (pageView.drawLayerDiv.offsetWidth - e.offsetX - 10) + "px;height:40px;position:absolute;line-height:inherit;word-break:break-all;opacity:1;resize:none;color:" + self.color + "\" placeholder=\"" + self.getDisplayText("annotation_placeholder") + "\"></textarea>";
                            var resizerDisplayRightString = "<div data-for=\"" + gs + "\" class=\"mouseOperationHint mouseOperationHintHorizontal\" style=\"left:" + (pageView.drawLayerDiv.offsetWidth - 7) + "px;top:" + (e.offsetY + 20) + "px\"></div>";
                            var resizerRightString = "<div data-for=\"" + gs + "\" class=\"textNoteResizerRight\" style=\"left:" + (pageView.drawLayerDiv.offsetWidth - 10 + resizerMarginRight) + "px;top:" + e.offsetY + "px;position:absolute;width:3px;height:40px\"></div>";
                            var bottomString = "<div data-for=\"" + gs + "\" class=\"textNoteBottom\" ></div>";
                            $(pageView.drawLayerDiv).append(divString + topMoverString + buttonString + cancelButtonString + "</div>" + textareaString + resizerDisplayRightString + resizerRightString + bottomString);
                            var textareaQuery = $(pageView.drawLayerDiv).children("textarea[data-for=\"" + gs + "\"]");

                            var resizeTextNote = function (c)
                            {
                                //文字批注，从文本框写入section标签
                                var value = c.val();
                                var txtContent = value.replace(/	/g, "　").trim();
                                c.val(txtContent);
                                var divContent = txtContent.replace(/\n/g, "<br/>").replace(/ /g, "&nbsp;").trim();
                                var sectionObject = self.creating;
                                sectionObject.contents.shapes.splice(0, sectionObject.contents.shapes.length);
                                var textObject = new Object();
                                textObject.color = self.color;
                                textObject.content = divContent;
                                textObject.type = sectionObject.type;
                                sectionObject.contents.shapes.push(textObject);
                                self.setAnnotationStyle(sectionObject);
                                var newHeight = $("#" + sectionObject.id).height() + 30;
                                c.height(newHeight);
                                $("div.textNoteResizerRight[data-for=\"" + sectionObject.id + "\"]").height(newHeight);
                                $("div.textNoteTitle[data-for=\"" + sectionObject.id + "\"]").height(titleHeight + newHeight + bottomHeight);
                            };

                            textareaQuery.change(function ()
                            {
                                var c = $(this);
                                resizeTextNote(c);
                            });
                            var resizer = $(pageView.drawLayerDiv).find("div.mouseOperationHint[data-for=\"" + gs + "\"]");
                            textareaQuery.mouseenter(function (e)
                            {
                                resizer.css("display", "block");
                            });
                            textareaQuery.mouseleave(function (e)
                            {
                                resizer.css("display", "none");
                            });
                            var textareaTitle = $(pageView.drawLayerDiv).children("div.textNoteTitle[data-for=\"" + gs + "\"]");
                            var resizerRight = $(pageView.drawLayerDiv).children("div.textNoteResizerRight[data-for=\"" + gs + "\"]");
                            var resizerDisplayRight = $(pageView.drawLayerDiv).children("div.mouseOperationHintHorizontal[data-for=\"" + gs + "\"]");
                            var ismoving;
                            var currentleft;
                            var currenttop;
                            var currentX;
                            var currentY;
                            var isResizing;
                            var currentWidth;
                            textareaTitle.mousedown(function (e1)
                            {
                                if (e1.target.nodeName === "BUTTON")
                                {
                                    return;
                                }
                                ismoving = true;
                                currentleft = this.offsetLeft;
                                currenttop = this.offsetTop;
                                currentX = e1.clientX;
                                currentY = e1.clientY;
                            });
                            resizerRight.mousedown(function (e1)
                            {
                                isResizing = true;
                                currentWidth = textareaQuery[0].offsetWidth;
                                currentX = e1.clientX;
                            });
                            $(pageView.drawLayerDiv).mousemove(function (e)
                            {
                                if (ismoving)
                                {
                                    var newLeft = currentleft + e.clientX - currentX;
                                    var parentWidth = textareaTitle.parent()[0].offsetWidth;
                                    if (newLeft < 0)
                                    {
                                        newLeft = 0;
                                    }
                                    if (newLeft + textareaTitle[0].offsetWidth > parentWidth)
                                    {
                                        newLeft = parentWidth - textareaTitle[0].offsetWidth;
                                    }
                                    var newTextareaQueryLeft = newLeft + paddingLeft;
                                    textareaTitle[0].style.left = newLeft + "px";
                                    textareaQuery[0].style.left = newTextareaQueryLeft + "px";
                                    sectionObject.width = (textareaTitle[0].offsetWidth - 5) / pageView.drawLayerDiv.offsetWidth;
                                    sectionObject.left = newTextareaQueryLeft / pageView.drawLayerDiv.offsetWidth;
                                    resizerRight[0].style.left = newTextareaQueryLeft + textareaQuery[0].offsetWidth + resizerMarginRight + "px";
                                    resizerDisplayRight[0].style.left = newTextareaQueryLeft + textareaQuery[0].offsetWidth + resizerMarginRight + "px";
                                    var newTop = currenttop + e.clientY - currentY;
                                    if (newTop < 0)
                                    {
                                        newTop = 0;
                                    }
                                    var parentHeight = textareaTitle.parent().parent()[0].offsetHeight - 20 - textareaQuery[0].offsetHeight;
                                    if (newTop + titleHeight + bottomHeight > parentHeight)
                                    {
                                        newTop = parentHeight - titleHeight - bottomHeight;
                                    }
                                    textareaTitle[0].style.top = newTop + "px";
                                    textareaQuery[0].style.top = newTop + titleHeight + "px";
                                    sectionObject.top = (newTop + titleHeight) / pageView.drawLayerDiv.offsetHeight;
                                    resizerRight[0].style.top = newTop + titleHeight + "px";
                                    resizerDisplayRight[0].style.top = newTop + titleHeight + resizerRight[0].offsetHeight / 2 + "px";
                                    self.setAnnotationStyle(sectionObject);
                                } else if (isResizing)
                                {
                                    var newWidth = currentWidth + e.clientX - currentX;
                                    if (newWidth > 100 && newWidth < pageView.drawLayerDiv.offsetWidth - textareaQuery[0].offsetLeft)
                                    {
                                        textareaTitle[0].style.width = (newWidth + paddingLeft) + "px";
                                        textareaQuery[0].style.width = newWidth + "px";
                                        sectionObject.width = (newWidth - 5) / pageView.drawLayerDiv.offsetWidth;
                                        resizerRight[0].style.left = textareaQuery[0].offsetLeft + newWidth + resizerMarginRight + "px";
                                        resizerDisplayRight[0].style.left = textareaQuery[0].offsetLeft + newWidth + resizerMarginRight + "px";
                                    }
                                    self.setAnnotationStyle(sectionObject);
                                }
                            });
                            $(pageView.drawLayerDiv).mouseleave(function (e)
                            {
                                if (ismoving)
                                {
                                    self.clearSelection();
                                    ismoving = false;
                                } else if (isResizing)
                                {
                                    self.clearSelection();
                                    isResizing = false;
                                }
                            });
                            $(pageView.drawLayerDiv).mouseup(function (e)
                            {
                                if (ismoving)
                                {
                                    self.clearSelection();
                                    ismoving = false;
                                } else if (isResizing)
                                {
                                    self.clearSelection();
                                    isResizing = false;
                                }
                            });

                            var buttonQuery = textareaTitle.children(".textNoteFinishButton");
                            buttonQuery.click(function ()
                            {
                                if (sectionObject.contents.shapes.length === 0 || sectionObject.contents.shapes.length !== 1 || sectionObject.contents.shapes[0].content == "")
                                {
                                    self.showMessageDialog(self.getDisplayText("annotation_type4_add_error"));
                                    return;
                                }
                                $(pageView.drawLayerDiv).empty();
                                self.creating = null;
                                $(pageView.drawLayerDiv).css("display", "none");
                                self.saveAnnotation(sectionObject);
                            });
                            var cancelButtonQuery = textareaTitle.children(".textNoteCancelButton");
                            cancelButtonQuery.click(function ()
                            {
                                $(pageView.drawLayerDiv).empty();
                                $(pageView.drawLayerDiv).css("display", "none");
                                self.resetDrawing();
                            });
                            textareaQuery.focus();
                            return false;
                        });
                        break;
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        //这里是画图的代码
                        $(annotationLayer).css("pointer-events", "auto");

                        $(annotationLayer).mousedown(function (e)
                        {
                            var obj = e.srcElement ? e.srcElement : e.target;
                            if (annotationLayer !== obj)
                            {
                                if ($(obj).hasClass("selectable"))
                                {
                                    return true;
                                }
                                return false;
                            }
                            self.pageToCreate = pageIndex;
                            self.mouseDownPoint = e;
                            var sectionLeft = e.offsetX / annotationLayer.offsetWidth;
                            var sectionTop = e.offsetY / annotationLayer.offsetHeight;
                            if (isNaN(sectionLeft) || isNaN(sectionTop))
                            {
                                self.resetDrawing();
                                return false;
                            }
                            var gs = self.createAnnotation(pageView.annotationLayer.div);
                            var sectionObject = new Object();
                            sectionObject.id = gs;
                            sectionObject.pageIndex = self.pageToCreate;
                            sectionObject.left = sectionLeft;
                            sectionObject.top = sectionTop;
                            sectionObject.editable = true;
                            sectionObject.type = self.annotationTypeToCreate;
                            sectionObject.contents = new Object();
                            sectionObject.contents.shapes = new Array();
                            sectionObject.contents.path = {
                                s: {
                                    x: e.offsetX,
                                    y: e.offsetY
                                },
                                v: 'm' + e.offsetX + ' ' + e.offsetY + ' '
                            };

                            var divObject = new Object();
                            divObject.color = self.color;
                            divObject.type = sectionObject.type;
                            sectionObject.contents.shapes.push(divObject);
                            self.creating = sectionObject;
                            $(annotationLayer).children(".edoc2annotation").removeClass("clickable").addClass("unclickable");
                        });
                        $(annotationLayer).mousemove(function (e)
                        {
                            if (self.creating == null)
                            {
                                return;
                            }
                            var sectionObject = self.creating;
                            if (e.pageX < self.mouseDownPoint.pageX)
                            {
                                sectionObject.left = (self.mouseDownPoint.offsetX - self.mouseDownPoint.pageX + e.pageX) / annotationLayer.offsetWidth;
                                sectionObject.width = (self.mouseDownPoint.pageX - e.pageX) / annotationLayer.offsetWidth;
                            } else
                            {
                                sectionObject.left = self.mouseDownPoint.offsetX / annotationLayer.offsetWidth;
                                sectionObject.width = (e.pageX - self.mouseDownPoint.pageX) / annotationLayer.offsetWidth;
                            }
                            if (e.pageY < self.mouseDownPoint.pageY)
                            {
                                sectionObject.top = (self.mouseDownPoint.offsetY - self.mouseDownPoint.pageY + e.pageY) / annotationLayer.offsetHeight;
                                sectionObject.height = (self.mouseDownPoint.pageY - e.pageY) / annotationLayer.offsetHeight;
                            } else
                            {
                                sectionObject.top = self.mouseDownPoint.offsetY / annotationLayer.offsetHeight;
                                sectionObject.height = (e.pageY - self.mouseDownPoint.pageY) / annotationLayer.offsetHeight;
                            }
                            switch (self.annotationTypeToCreate)
                            {
                                case 7:
                                    sectionObject.contents.shapes.splice(0, sectionObject.contents.shapes.length);
                                    var r = Math.sqrt(Math.pow(self.mouseDownPoint.pageY - e.pageY, 2) + Math.pow(self.mouseDownPoint.pageX - e.pageX, 2));
                                    var sin1 = (e.pageY - self.mouseDownPoint.pageY) / r;
                                    var cos1 = (e.pageX - self.mouseDownPoint.pageX) / r;
                                    var divObject = new Object();
                                    divObject.color = self.color;
                                    divObject.type = sectionObject.type;
                                    divObject.sin = sin1;
                                    divObject.cos = cos1;
                                    sectionObject.contents.shapes.push(divObject);
                                    break;
                                case 8:
                                    var p = sectionObject.contents.path;

                                    if (p)
                                    {

                                        sectionObject.width = 1;
                                        sectionObject.height = 1;
                                        sectionObject.top = 0;
                                        sectionObject.left = 0;

                                        sectionObject.contents.path = {
                                            s: {
                                                x: e.offsetX,
                                                y: e.offsetY
                                            },
                                            v: p.v + 'C' + p.s.x + ' ' + p.s.y + ' ' + e.offsetX + ' ' + e.offsetY + ' ' + e.offsetX + ' ' + e.offsetY+' '
                                        }
                                    }
                                    break;
                            }
                            self.setAnnotationStyle(sectionObject);
                        });
                        $(annotationLayer).mouseleave(function (e)
                        {
                            if (self.creating != null)
                            {
                                self.resetDrawing();
                                $(annotationLayer).children(".edoc2annotation").removeClass("unclickable").addClass("clickable");
                                return;
                            }
                        });
                        $(annotationLayer).mouseup(function (e)
                        {
                            if (pageIndex !== self.pageToCreate)
                            {
                                //我们不支持跨页的创建批注。
                                self.resetDrawing();
                                $(annotationLayer).children(".edoc2annotation").removeClass("unclickable").addClass("clickable");
                                return;
                            }
                            if (self.creating != null)
                            {
                                var sectionObject = self.creating;
                                var creatingSection = $("#" + sectionObject.id)[0];
                                if (creatingSection.offsetWidth <= 5 && creatingSection.offsetHeight <= 5)
                                {   //这个点太小了，可能是误操作
                                    self.resetDrawing();
                                    $(annotationLayer).children(".edoc2annotation").removeClass("unclickable").addClass("clickable");
                                    return;
                                }
                                self.saveAnnotation(sectionObject);
                                self.creating = null;
                                $(annotationLayer).children(".edoc2annotation").removeClass("unclickable").addClass("clickable");
                            }
                        });
                        break;
                }
            },

            removeDrawPanel: function ()
            {
                for (var i = 0; i < window.PDFViewerApplication.pdfViewer.pagesCount; i++)
                {
                    var pageView = window.PDFViewerApplication.pdfViewer.getPageView(i);
                    if (pageView.annotationLayer != null && pageView.annotationLayer.div != null)
                    {
                        var annotationLayer = pageView.annotationLayer.div;
                        $(annotationLayer).css("pointer-events", "none");
                        $(annotationLayer).unbind("mousedown");
                        $(annotationLayer).unbind("mousemove");
                        $(annotationLayer).unbind("mouseleave");
                        $(annotationLayer).unbind("mouseup");
                    }
                    if (pageView.drawLayerDiv != null)
                    {
                        $(pageView.drawLayerDiv).remove();
                        pageView.drawLayerDiv = null;
                    }
                }
            },

            setTypeToCreate: function (t)
            {
                switch (this.annotationTypeToCreate)
                {
                    case 4:
                        if (this.creating != null)
                        {
                            this.resetDrawing();
                        }
                        break;
                }
                this.annotationTypeToCreate = t;
                this.removeDrawPanel();
                switch (this.annotationTypeToCreate)
                {
                    case 1:
                        this.setColor("#FFFF00");
                        unblockWnd(window);
                        break;
                    case 2:
                        this.setColor("#00B050");
                        unblockWnd(window);
                        break;
                    case 3:
                        this.setColor("#FF0000");
                        unblockWnd(window);
                        break;
                    case 4:
                        this.setColor("#FF0000");
                        unblockWnd(window);
                        this.prepareForDrawingOnEveryPage();
                        break;
                    case 5:
                        this.setColor(this.drawToolColor);
                        reblockWnd(window);
                        this.prepareForDrawingOnEveryPage();
                        break;
                    case 6:
                        this.setColor(this.drawToolColor);
                        reblockWnd(window);
                        this.prepareForDrawingOnEveryPage();
                        break;
                    case 7:
                        this.setColor(this.drawToolColor);
                        reblockWnd(window);
                        this.prepareForDrawingOnEveryPage();
                        break;
                    case 8:
                        this.setColor(this.drawToolColor);
                        reblockWnd(window);
                        this.prepareForDrawingOnEveryPage();
                        break;
                    case 9:
                        this.setColor(this.drawToolColor);
                        reblockWnd(window);
                        this.prepareForDrawingOnEveryPage();
                        break;
                }
            },

            getCurrentPageAnnotations: function (pageIndex)
            {

            },

            createAnnotation: function (annotationLayer)
            {
                var guid = Guid.NewGuid();
                var gs = guid.ToString("N");
                $(annotationLayer).append("<section class=\"edoc2annotation clickable\" id=\"" + gs + "\"></section>");
                return gs;
            },

            renderAnnotation: function (sectionObject)
            {
                var pageView = window.PDFViewerApplication.pdfViewer.getPageView(sectionObject.pageIndex);
                var annotationLayer = pageView.annotationLayer.div;
                $(annotationLayer).append("<section class=\"edoc2annotation clickable\" id=\"" + sectionObject.id + "\"></section>");
                this.makeAnnotationClickable(sectionObject);
            },

            makeAnnotationClickable: function (sectionObject)
            {
                var sectionElement = $("#" + sectionObject.id);
                var events = sectionElement.data("events");
                if (!events || !events["click"])
                {
                    sectionElement.click(function (e)
                    {
                        var obj = e.srcElement ? e.srcElement : e.target;
                        if ($(obj).hasClass("edoc2MoveAndResize"))
                        {
                            return false;
                        }
                        $(document).trigger("OnAnnotationClick", sectionObject);
                        return false;
                    });
                }
                if (!events || !events["mouseenter"])
                {
                    sectionElement.mouseenter(function (e)
                    {
                        sectionElement.find(".mouseOperationHint").css("display", "block");
                        var obj = e.srcElement ? e.srcElement : e.target;
                        if ($(obj).hasClass("edoc2MoveAndResize"))
                        {
                            $(document).trigger("OnAnnotationLeave", sectionObject);
                            return false;
                        }
                        if (sectionObject.type === 7 && sectionObject.contents.shapes.length > 0)
                        {
                            var sectionDiv = $("#" + sectionObject.id)[0];
                            var suggestOffset = {};
                            //箭头有个特点：它在矩形的屏幕上看上去不封闭，因此悬浮窗的出现位置如果按照其它图案的方式计算的话，看上去会有些远。
                            var divObject = sectionObject.contents.shapes[0];
                            if (divObject.cos !== 0)
                            {
                                if ($(obj).hasClass("rightArrow"))
                                {
                                    var line = $(obj).prev()[0];
                                    suggestOffset.offsetX = sectionDiv.offsetLeft + 15 + line.offsetWidth * divObject.cos;
                                    suggestOffset.offsetY = sectionDiv.offsetTop + line.offsetWidth * divObject.sin - 10;
                                } else
                                {
                                    suggestOffset.offsetX = sectionDiv.offsetLeft + 15 + e.offsetX * divObject.cos;
                                    suggestOffset.offsetY = sectionDiv.offsetTop + e.offsetX * divObject.sin - 10;
                                }
                                if (divObject.sin < 0)
                                {
                                    suggestOffset.offsetY = suggestOffset.offsetY + sectionDiv.offsetHeight;
                                }
                                if (divObject.cos < 0)
                                {
                                    suggestOffset.offsetX = suggestOffset.offsetX + sectionDiv.offsetWidth;
                                }

                                $(document).trigger("OnAnnotationEnter", [sectionObject, suggestOffset]);
                                return false;
                            }
                        }
                        $(document).trigger("OnAnnotationEnter", sectionObject);
                        return false;
                    });
                }
                if (!events || !events["mouseleave"])
                {
                    sectionElement.mouseleave(function (e)
                    {
                        sectionElement.find(".mouseOperationHint").css("display", "none");
                        //var obj = e.srcElement ? e.srcElement : e.target;
                        //if ($(obj).hasClass("edoc2MoveAndResize")) {
                        //    return false;
                        //}
                        $(document).trigger("OnAnnotationLeave", sectionObject);
                        return false;
                    });
                }
            },

            setAnnotationStyle: function (sectionObject)
            {
                var self = this;
                var sectionElement = $("#" + sectionObject.id);

                var pageView = window.PDFViewerApplication.pdfViewer.getPageView(sectionObject.pageIndex);

                //这是最为关键渲染批注的方法，传入sectionObject，这个方法将把它渲染在页面上
                sectionElement.css("left", sectionObject.left * 100 + "%").css("top", sectionObject.top * 100 + "%");
                if (sectionObject.type === 4)
                {
                    sectionElement.css("max-width", (1 - sectionObject.left) * 100 + "%");
                    sectionElement.css("word-break", "break-all");
                    sectionElement.css("border", "1px solid transparent");
                    if (sectionObject.width != null && sectionObject.width !== 0)
                    {
                        sectionElement.css("width", sectionObject.width * 100 + "%");
                    }
                } else
                {
                    sectionElement.css("pointer-events", "none");
                    if (sectionObject.width != null)
                    {
                        sectionElement.css("width", sectionObject.width * 100 + "%");
                    }
                    if (sectionObject.height != null)
                    {
                        sectionElement.css("height", sectionObject.height * 100 + "%");
                    }
                }

                sectionElement.empty();
                for (var i = 0; i < sectionObject.contents.shapes.length; i++)
                {
                    var divObject = sectionObject.contents.shapes[i];
                    switch (divObject.type)
                    {
                        case 1:
                            var s1 = "opacity:0.5;-webkit-transform-origin: 0 0 0;-moz-transform-origin: 0 0 0;-ms-transform-origin: 0 0 0;-o-transform-origin: 0 0 0;transform-origin: 0 0 0;background-color:" + divObject.color + ";left:" + divObject.left * 100 + "%;top:" + divObject.top * 100 + "%;width:" + divObject.width * 100 + "%;height:" + divObject.height * 100 + "%";
                            if (divObject.sin !== 0 || (divObject.cos !== 0 && divObject.cos !== 1))
                            {
                                var acos = Math.acos(divObject.cos);
                                if (divObject.sin < 0)
                                {
                                    acos = 2 * Math.PI - acos;
                                }
                                var rotate1 = "rotate(" + acos / Math.PI * 180 + "deg)";
                                s1 += ";-webkit-transform:" + rotate1;
                                s1 += ";-moz-transform:" + rotate1;
                                s1 += ";-ms-transform:" + rotate1;
                                s1 += ";-o-transform:" + rotate1;
                                s1 += ";transform:" + rotate1;
                            }
                            sectionElement.append("<div style=\"" + s1 + "\"></div>");
                            break;
                        case 2:
                            var s2 = "-webkit-transform-origin: 0 0 0;-moz-transform-origin: 0 0 0;-ms-transform-origin: 0 0 0;-o-transform-origin: 0 0 0;transform-origin: 0 0 0;border-bottom:1px solid " + divObject.color + ";left:" + divObject.left * 100 + "%;top:" + divObject.top * 100 + "%;width:" + divObject.width * 100 + "%;height:" + divObject.height * 100 + "%";
                            if (divObject.sin !== 0 || (divObject.cos !== 0 && divObject.cos !== 1))
                            {
                                var acos = Math.acos(divObject.cos);
                                if (divObject.sin < 0)
                                {
                                    acos = 2 * Math.PI - acos;
                                }
                                var rotate2 = "rotate(" + acos / Math.PI * 180 + "deg)";
                                s2 += ";-webkit-transform:" + rotate2;
                                s2 += ";-moz-transform:" + rotate2;
                                s2 += ";-ms-transform:" + rotate2;
                                s2 += ";-o-transform:" + rotate2;
                                s2 += ";transform:" + rotate2;
                            }
                            sectionElement.append("<div style=\"" + s2 + "\"></div>");
                            break;
                        case 3:
                            var s3 = "-webkit-transform-origin: 0 0 0;-moz-transform-origin: 0 0 0;-ms-transform-origin: 0 0 0;-o-transform-origin: 0 0 0;transform-origin: 0 0 0;border-bottom:1px solid " + divObject.color + ";left:" + divObject.left * 100 + "%;top:" + divObject.top * 100 + "%;width:" + divObject.width * 100 + "%;height:" + divObject.height * 50 + "%";
                            var s32 = "-webkit-transform-origin: 0 0 0;-moz-transform-origin: 0 0 0;-ms-transform-origin: 0 0 0;-o-transform-origin: 0 0 0;transform-origin: 0 0 0;left:" + divObject.left * 100 + "%;top:" + (divObject.top * 100 + divObject.height * 50) + "%;width:" + divObject.width * 100 + "%;height:" + divObject.height * 50 + "%";
                            if (divObject.sin !== 0 || (divObject.cos !== 0 && divObject.cos !== 1))
                            {
                                var acos = Math.acos(divObject.cos);
                                if (divObject.sin < 0)
                                {
                                    acos = 2 * Math.PI - acos;
                                }
                                var rotate2 = "rotate(" + acos / Math.PI * 180 + "deg)";
                                s3 += ";-webkit-transform:" + rotate2;
                                s3 += ";-moz-transform:" + rotate2;
                                s3 += ";-ms-transform:" + rotate2;
                                s3 += ";-o-transform:" + rotate2;
                                s3 += ";transform:" + rotate2;

                                s32 += ";-webkit-transform:" + rotate2;
                                s32 += ";-moz-transform:" + rotate2;
                                s32 += ";-ms-transform:" + rotate2;
                                s32 += ";-o-transform:" + rotate2;
                                s32 += ";transform:" + rotate2;
                            }
                            sectionElement.append("<div style=\"" + s3 + "\"></div><div style=\"" + s32 + "\"></div>");
                            break;
                        case 4:
                            sectionElement.css("color", divObject.color);
                            sectionElement.html(divObject.content);
                            if (sectionObject.editable)
                            {
                                this.makeAnnotationMovable(sectionObject);

                                sectionElement.append("<div class=\"mouseOperationHint mouseOperationHintHorizontal\" style=\"left:100%;top:50%\"></div>");
                                sectionElement.append("<div style=\"left:100%;top:0;position:absolute;width:6px;height:100%;cursor:w-resize;margin:0 -3px 0 -3px\"></div>");
                                var resizerRight7 = sectionElement.children(":last");
                                resizerRight7.addClass("edoc2MoveAndResize");
                                var isResizing7;
                                var currentX7;
                                var currentWidth7;
                                resizerRight7.mousedown(function (e)
                                {
                                    isResizing7 = true;
                                    currentWidth7 = sectionElement[0].offsetWidth;
                                    currentX7 = e.clientX;
                                    $(this).css("z-index", "201");
                                    $(this).css("width", pageView.annotationLayer.div.offsetWidth + "px");
                                    $(this).css("height", pageView.annotationLayer.div.offsetHeight + "px");
                                    $(this).css("top", "50%");
                                    $(this).css("margin", "-" + (pageView.annotationLayer.div.offsetHeight / 2) + "px -" + (pageView.annotationLayer.div.offsetWidth / 2) + "px");
                                    //$(this).css("background-color", "black");
                                    return false;
                                });
                                resizerRight7.mousemove(function (e)
                                {
                                    if (isResizing7)
                                    {
                                        var newWidth = currentWidth7 + e.clientX - currentX7;
                                        if (newWidth > 25 && newWidth + sectionElement[0].offsetLeft + 5 <= pageView.annotationLayer.div.offsetWidth)
                                        {
                                            sectionObject.width = newWidth / pageView.annotationLayer.div.offsetWidth;
                                            sectionElement.css("width", (newWidth / pageView.annotationLayer.div.offsetWidth) * 100 + "%");
                                        }
                                    }
                                    return false;
                                });
                                resizerRight7.mouseleave(function (e)
                                {
                                    if (isResizing7)
                                    {
                                        isResizing7 = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "6px");
                                        $(this).css("top", "0");
                                        $(this).css("height", "100%");
                                        $(this).css("margin", "0 -3px 0 -3px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                        return false;
                                    }
                                    return true;
                                });
                                resizerRight7.mouseup(function (e)
                                {
                                    if (isResizing7)
                                    {
                                        isResizing7 = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "6px");
                                        $(this).css("top", "0");
                                        $(this).css("height", "100%");
                                        $(this).css("margin", "0 -3px 0 -3px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                    }
                                    return false;
                                });
                            }
                            break;
                        case 5:
                            sectionElement.append("<div style=\"border:2px solid transparent;background-color:transparent;margin:-2px;position:absolute;left:0;top:0;width:100%;height:1px\"></div>");
                            sectionElement.append("<div style=\"border:2px solid transparent;background-color:transparent;margin:-2px;position:absolute;left:0;top:0;width:1px;height:100%\"></div>");
                            sectionElement.append("<div style=\"border:2px solid transparent;background-color:transparent;margin:-1px -2px -2px -1px;position:absolute;left:100%;top:0;width:1px;height:100%\"></div>");
                            sectionElement.append("<div style=\"border:2px solid transparent;background-color:transparent;margin:-1px -2px -2px -1px;position:absolute;left:0;top:100%;width:100%;height:1px\"></div>");
                            sectionElement.append("<div style=\"border:1px solid " + divObject.color + ";left:0;top:0;width:100%;height:100%;pointer-events: none\"></div>");
                            if (sectionObject.editable)
                            {
                                this.makeAnnotationMovable(sectionObject);

                                sectionElement.append("<div class=\"mouseOperationHint mouseOperationHintCircle\" style=\"left:100%;top:100%\"></div>");
                                sectionElement.append("<div style=\"background-color:transparent;margin:-5px;position:absolute;left:100%;top:100%;width:10px;height:10px;z-index:100;cursor:se-resize\"></div>");
                                var bottomResizeBar = sectionElement.children(":last");
                                bottomResizeBar.addClass("edoc2MoveAndResize");
                                var isResizing;
                                var currentX2;
                                var currentY2;
                                var currentWidth;
                                var currentHeight;
                                bottomResizeBar.mousedown(function (e)
                                {
                                    isResizing = true;
                                    currentWidth = sectionElement[0].offsetWidth;
                                    currentHeight = sectionElement[0].offsetHeight;
                                    currentX2 = e.clientX;
                                    currentY2 = e.clientY;
                                    $(this).css("z-index", "101");
                                    $(this).css("width", "20px");
                                    $(this).css("height", "20px");
                                    $(this).css("margin", "-10px");
                                    //$(this).css("background-color", "black");
                                    return false;
                                });
                                bottomResizeBar.mousemove(function (e)
                                {
                                    if (isResizing)
                                    {
                                        var newWidth = currentWidth + e.clientX - currentX2;
                                        if (newWidth > 5 && newWidth + sectionElement[0].offsetLeft + 5 <= pageView.annotationLayer.div.offsetWidth)
                                        {
                                            sectionObject.width = newWidth / pageView.annotationLayer.div.offsetWidth;
                                            sectionElement.css("width", (newWidth / pageView.annotationLayer.div.offsetWidth) * 100 + "%");
                                        }
                                        var newHeight = currentHeight + e.clientY - currentY2;
                                        if (newHeight > 5 && newHeight + sectionElement[0].offsetTop + 5 <= pageView.annotationLayer.div.offsetHeight)
                                        {
                                            sectionObject.height = newHeight / pageView.annotationLayer.div.offsetHeight;
                                            sectionElement.css("height", (newHeight / pageView.annotationLayer.div.offsetHeight) * 100 + "%");
                                        }
                                    }
                                    return false;
                                });
                                bottomResizeBar.mouseleave(function (e)
                                {
                                    if (isResizing)
                                    {
                                        isResizing = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "10px");
                                        $(this).css("height", "10px");
                                        $(this).css("margin", "-5px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                        return false;
                                    }
                                    return true;
                                });
                                bottomResizeBar.mouseup(function (e)
                                {
                                    if (isResizing)
                                    {
                                        isResizing = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "10px");
                                        $(this).css("height", "10px");
                                        $(this).css("margin", "-5px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                    }
                                    return false;
                                });
                            }
                            break;
                        case 6:
                            sectionElement.append("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"100%\" height=\"100%\"><ellipse cx=\"50%\" cy=\"50%\" rx=\"50%\" ry=\"50%\" stroke=\"white\" fill=\"transparent\" stroke-width=\"5\" style=\"cursor:pointer;pointer-events:visibleStroke\"/><ellipse cx=\"50%\" cy=\"50%\" rx=\"50%\" ry=\"50%\" stroke=\"" + divObject.color + "\" fill=\"transparent\" stroke-width=\"1\" style=\"cursor:pointer;pointer-events:none\"/></svg>");
                            if (sectionObject.editable)
                            {
                                this.makeAnnotationMovable(sectionObject);

                                sectionElement.append("<div class=\"mouseOperationHint mouseOperationHintCircle\" style=\"left:85.355339%;top:85.355339%\"></div>");
                                sectionElement.append("<div style=\"background-color:transparent;margin:-5px;position:absolute;left:85.355339%;top:85.355339%;width:10px;height:10px;z-index:100;cursor:se-resize\"></div>");
                                var bottomResizeBar4 = sectionElement.children(":last");
                                bottomResizeBar4.addClass("edoc2MoveAndResize");
                                var isResizing4;
                                var currentX4;
                                var currentY4;
                                var currentWidth4;
                                var currentHeight4;
                                bottomResizeBar4.mousedown(function (e)
                                {
                                    isResizing4 = true;
                                    currentWidth4 = sectionElement[0].offsetWidth;
                                    currentHeight4 = sectionElement[0].offsetHeight;
                                    currentX4 = e.clientX;
                                    currentY4 = e.clientY;
                                    $(this).css("z-index", "101");
                                    $(this).css("width", "100px");
                                    $(this).css("height", "100px");
                                    $(this).css("margin", "-50px");
                                    //$(this).css("background-color", "black");
                                    return false;
                                });
                                bottomResizeBar4.mousemove(function (e)
                                {
                                    if (isResizing4)
                                    {
                                        var newWidth = currentWidth4 + (e.clientX - currentX4) / 0.85355339;
                                        if (newWidth > 5 && newWidth + sectionElement[0].offsetLeft + 5 <= pageView.annotationLayer.div.offsetWidth)
                                        {
                                            sectionObject.width = newWidth / pageView.annotationLayer.div.offsetWidth;
                                            sectionElement.css("width", (newWidth / pageView.annotationLayer.div.offsetWidth) * 100 + "%");
                                        }
                                        var newHeight = currentHeight4 + (e.clientY - currentY4) / 0.85355339;
                                        if (newHeight > 5 && newHeight + sectionElement[0].offsetTop + 5 <= pageView.annotationLayer.div.offsetHeight)
                                        {
                                            sectionObject.height = newHeight / pageView.annotationLayer.div.offsetHeight;
                                            sectionElement.css("height", (newHeight / pageView.annotationLayer.div.offsetHeight) * 100 + "%");
                                        }
                                    }
                                    return false;
                                });
                                bottomResizeBar4.mouseleave(function (e)
                                {
                                    if (isResizing4)
                                    {
                                        isResizing4 = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "10px");
                                        $(this).css("height", "10px");
                                        $(this).css("margin", "-5px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                        return false;
                                    }
                                    return true;
                                });
                                bottomResizeBar4.mouseup(function (e)
                                {
                                    if (isResizing4)
                                    {
                                        isResizing4 = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "10px");
                                        $(this).css("height", "10px");
                                        $(this).css("margin", "-5px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                    }
                                    return false;
                                });
                            }
                            break;
                        case 7:
                            if (divObject.cos === 0)
                            {
                                var div3 = "<div style=\"background-color:" + divObject.color + ";height:100%;width:1px;left:0%;top:0%;position:absolute;margin:-1px;border:1px solid white\"></div>";
                                var div4;
                                if (divObject.sin > 0)
                                {
                                    div4 = "<div class=\"bottomArrow\" style=\"border-top-color:" + divObject.color + ";left:0%;top:100%;position:absolute\"></div>";
                                } else
                                {
                                    div4 = "<div class=\"topArrow\" style=\"border-bottom-color:" + divObject.color + ";left:0%;top:0%;position:absolute\"></div>";
                                }
                                sectionElement.append("<div style=\"left:0%;top:0%;width:100%;height:100%;pointer-events: none\">" + div3 + div4 + "</div>");
                            } else
                            {
                                var acos = Math.acos(divObject.cos);
                                if (divObject.sin < 0)
                                {
                                    acos = 2 * Math.PI - acos;
                                }
                                var rotate1 = "rotate(" + acos / Math.PI * 180 + "deg)";
                                var width1;
                                width1 = Math.abs(1 / divObject.cos);
                                var s7 = "";
                                s7 += ";-webkit-transform:" + rotate1;
                                s7 += ";-moz-transform:" + rotate1;
                                s7 += ";-ms-transform:" + rotate1;
                                s7 += ";-o-transform:" + rotate1;
                                s7 += ";transform:" + rotate1;

                                var div1 = "<div style=\"background-color:" + divObject.color + ";height:1px;width:" + width1 * 100 + "%;left:" + (1 - width1) * 50 + "%;top:50%;position:absolute;margin:-1px -2px -1px -1px;border:1px solid white\"></div>";
                                var div2 = "<div class=\"rightArrow\" style=\"border-left-color:" + divObject.color + ";left:" + (1 + width1) * 50 + "%;top:50%;position:absolute\"></div>";
                                sectionElement.append("<div style=\"" + s7 + ";left:0%;top:0%;width:100%;height:100%;pointer-events: none\">" + div1 + div2 + "</div>");
                            }
                            if (sectionObject.editable)
                            {
                                this.makeAnnotationMovable(sectionObject);
                            }
                            break;
                        case 8:
                            var p = sectionObject.contents.path;

                            sectionElement.append("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"100%\" height=\"100%\">" +
                                "<path d=\"" + p.v + "\" style=\"fill: transparent\"; stroke-width: 5; stroke: white;\" />" +
                                "<path d=\"" + p.v + "\" style=\"fill: none; stroke: " + divObject.color + "; stroke-width: 1\" />" +
                                "</svg > ");
                            if (sectionObject.editable)
                            {
                                this.makeAnnotationMovable(sectionObject);
                                sectionElement.append("<div class=\"mouseOperationHint mouseOperationHintCircle\" style=\"left:85.355339%;top:85.355339%\"></div>");
                            }

                            break;
                        case 9:
                            sectionElement.append("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 1024 1024\" version=\"1.1\" width=\"100%\" height=\"100%\">" +
                                "<g><path d=\"M10.750656 1013.12136c-13.822272-13.822272-13.822272-36.347457 0-50.169729l952.200975-952.200975c13.822272-13.822272 36.347457-13.822272 50.169729 0 13.822272 13.822272 13.822272 36.347457 0 50.169729l-952.200975 952.200975c-14.334208 14.334208-36.347457 14.334208-50.169729 0z\" stroke=\"white\" fill=\"transparent\" stroke-width=\"5\" style=\"cursor:pointer;pointer-events:visibleStroke\"/>" +
                                "<path d=\"M10.750656 10.750656c13.822272-13.822272 36.347457-13.822272 50.169729 0L1013.633296 963.463567c13.822272 13.822272 13.822272 36.347457 0 50.169729-13.822272 13.822272-36.347457 13.822272-50.169729 0L10.750656 60.920385c-14.334208-14.334208-14.334208-36.347457 0-50.169729z\" stroke=\"white\" fill=\"transparent\" stroke-width=\"5\" style=\"cursor:pointer;pointer-events:visibleStroke\"/></g>" +
                                "<g><path d=\"M10.750656 1013.12136c-13.822272-13.822272-13.822272-36.347457 0-50.169729l952.200975-952.200975c13.822272-13.822272 36.347457-13.822272 50.169729 0 13.822272 13.822272 13.822272 36.347457 0 50.169729l-952.200975 952.200975c-14.334208 14.334208-36.347457 14.334208-50.169729 0z\" fill=\"" + divObject.color + "\" fill=\"transparent\" stroke-width=\"1\" style=\"cursor:pointer;pointer-events:none\"/>" +
                                "<path d=\"M10.750656 10.750656c13.822272-13.822272 36.347457-13.822272 50.169729 0L1013.633296 963.463567c13.822272 13.822272 13.822272 36.347457 0 50.169729-13.822272 13.822272-36.347457 13.822272-50.169729 0L10.750656 60.920385c-14.334208-14.334208-14.334208-36.347457 0-50.169729z\" fill=\"" + divObject.color + "\" fill=\"transparent\" stroke-width=\"1\" style=\"cursor:pointer;pointer-events:none\"/></g>" +
                                "</svg>");
                            if (sectionObject.editable)
                            {
                                this.makeAnnotationMovable(sectionObject);

                                sectionElement.append("<div class=\"mouseOperationHint mouseOperationHintCircle\" style=\"left:85.355339%;top:85.355339%\"></div>");
                                sectionElement.append("<div style=\"background-color:transparent;margin:-5px;position:absolute;left:85.355339%;top:85.355339%;width:10px;height:10px;z-index:100;cursor:se-resize\"></div>");
                                var bottomResizeBar4 = sectionElement.children(":last");
                                bottomResizeBar4.addClass("edoc2MoveAndResize");
                                var isResizing4;
                                var currentX4;
                                var currentY4;
                                var currentWidth4;
                                var currentHeight4;
                                bottomResizeBar4.mousedown(function (e)
                                {
                                    isResizing4 = true;
                                    currentWidth4 = sectionElement[0].offsetWidth;
                                    currentHeight4 = sectionElement[0].offsetHeight;
                                    currentX4 = e.clientX;
                                    currentY4 = e.clientY;
                                    $(this).css("z-index", "101");
                                    $(this).css("width", "100px");
                                    $(this).css("height", "100px");
                                    $(this).css("margin", "-50px");
                                    return false;
                                });
                                bottomResizeBar4.mousemove(function (e)
                                {
                                    if (isResizing4)
                                    {
                                        var newWidth = currentWidth4 + (e.clientX - currentX4) / 0.85355339;
                                        if (newWidth > 5 && newWidth + sectionElement[0].offsetLeft + 5 <= pageView.annotationLayer.div.offsetWidth)
                                        {
                                            sectionObject.width = newWidth / pageView.annotationLayer.div.offsetWidth;
                                            sectionElement.css("width", (newWidth / pageView.annotationLayer.div.offsetWidth) * 100 + "%");
                                        }
                                        var newHeight = currentHeight4 + (e.clientY - currentY4) / 0.85355339;
                                        if (newHeight > 5 && newHeight + sectionElement[0].offsetTop + 5 <= pageView.annotationLayer.div.offsetHeight)
                                        {
                                            sectionObject.height = newHeight / pageView.annotationLayer.div.offsetHeight;
                                            sectionElement.css("height", (newHeight / pageView.annotationLayer.div.offsetHeight) * 100 + "%");
                                        }
                                    }
                                    return false;
                                });
                                bottomResizeBar4.mouseleave(function (e)
                                {
                                    if (isResizing4)
                                    {
                                        isResizing4 = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "10px");
                                        $(this).css("height", "10px");
                                        $(this).css("margin", "-5px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                        return false;
                                    }
                                    return true;
                                });
                                bottomResizeBar4.mouseup(function (e)
                                {
                                    if (isResizing4)
                                    {
                                        isResizing4 = false;
                                        $(this).css("z-index", "100");
                                        $(this).css("width", "10px");
                                        $(this).css("height", "10px");
                                        $(this).css("margin", "-5px");
                                        $(this).css("background-color", "transparent");
                                        self.editAnnotationSizeAndLocation(sectionObject);
                                    }
                                    return false;
                                });
                            }
                            break;
                        default:
                            break;
                    }
                }
            },

            saveAnnotation: function (sectionObject)
            {
                var self = this;
                var fileId = this.previewData.fileId;
                var fileVerId = this.previewData.fileVerId;
                sectionObject.fileId = fileId;
                sectionObject.fileVerId = fileVerId;
                var json = JSON.stringify(sectionObject);
                $.ajax({
                    data: {
                        module: "WebClient",
                        fun: "AddAnnotation",
                        sectionJson: json
                    }
                }).done(function (data)
                {
                    var result = JSON.parse(data.obj);
                    if (sectionObject.id == result.id)
                    {
                        //保存出错了。因为如果保存成功，返回值的id将变为数据库里存储的主键。
                        $("#" + sectionObject.id).remove();
                        return;
                    }
                    $("#" + sectionObject.id).attr("id", result.id);
                    sectionObject.id = result.id;
                    sectionObject.userId = result.userId;
                    sectionObject.userName = result.userName;
                    sectionObject.dateTimeCreated = result.dateTimeCreated;
                    sectionObject.firstCommentId = result.firstCommentId;
                    self.makeAnnotationClickable(result);

                    $(document).trigger("OnAnnotationRendered", result);
                    $(document).trigger("OnAnnotationCreated", result);
                });
            },

            editAnnotationSizeAndLocation: function (sectionObject)
            {
                var json = JSON.stringify(sectionObject);
                $.ajax({
                    data: {
                        module: "WebClient",
                        fun: "EditAnnotationSizeAndLocation",
                        sectionJson: json
                    }
                }).done(function (data)
                {
                    var result = JSON.parse(data.obj);
                });
            },

            deleteAnnotation: function (sectionId, callback)
            {
                $.ajax({
                    data: {
                        module: "WebClient",
                        fun: "DeleteAnnotation",
                        sectionId: sectionId
                    }
                }).done(function (data)
                {
                    $("#" + sectionId).remove();
                    $(document).trigger("OnAnnotationDeleted", sectionId);
                    if (callback)
                    {
                        callback();
                    }
                });
            },

            toggleDisplayAnnotation: function ()
            {
                if (this.displayAnnotation)
                {
                    this.closeDisplayAnnotation();
                } else
                {
                    this.openDisplayAnnotation();
                }
            },

            openDisplayAnnotation: function ()
            {
                if (this.displayAnnotation)
                {
                    return;
                }
                this.displayAnnotation = true;
                $(document).trigger("OnDisplayAnnotationOpened");
            },

            closeDisplayAnnotation: function ()
            {
                if (!this.displayAnnotation)
                {
                    return;
                }
                if (this.annotationbar == null || this.annotationbar.opened)
                {
                    return;
                }
                this.displayAnnotation = false;
                $(document).trigger("OnDisplayAnnotationClosed");
            },

            loadAnnotationOnEveryPage: function ()
            {
                for (var i = 0; i < window.PDFViewerApplication.pdfViewer.pagesCount; i++)
                {
                    var pageView = window.PDFViewerApplication.pdfViewer.getPageView(i);
                    if (pageView.loadingIconDiv == null)
                    {
                        this.loadAnnotation(i);
                    }
                }
            },

            loadAnnotation: function (pageIndex)
            {
                var pageView = window.PDFViewerApplication.pdfViewer.getPageView(pageIndex);
                if ($(pageView.annotationLayer.div).children(".edoc2annotation").length !== 0)
                {
                    return;
                }
                var self = this;
                var fileVerId = this.previewData.fileVerId;
                $.ajax({
                    data: {
                        module: "WebClient",
                        fun: "GetAnnotationList",
                        fileVerId: fileVerId,
                        pageIndex: pageIndex
                    }
                }).done(function (data)
                {
                    var result = JSON.parse(data.obj);
                    for (var i = 0; i < result.length; i++)
                    {
                        self.renderAnnotation(result[i]);
                        self.setAnnotationStyle(result[i]);
                        $(document).trigger("OnAnnotationRendered", result[i]);
                    }
                });
            },

            removeAnnotationOnEveryPage: function ()
            {
                $(".edoc2annotation").remove();
            },

            makeAnnotationMovable: function (sectionObject)
            {
                var self = this;
                var sectionElement = $("#" + sectionObject.id);
                var pageView = window.PDFViewerApplication.pdfViewer.getPageView(sectionObject.pageIndex);
                //这是个并没有什么用的，只起提示作用的东西。
                if (sectionObject.type === 7)
                {
                    //箭头这个东西比较特殊。
                    sectionElement.append("<div class=\"mouseOperationHint mouseOperationHintRectangle\" style=\"left:50%;top:50%\"></div>");
                } else
                {
                    sectionElement.append("<div class=\"mouseOperationHint mouseOperationHintRectangle\" style=\"left:50%;top:0\"></div>");
                }
                sectionElement.append("<div style=\"background-color:transparent;position:absolute;cursor:move\"></div>");
                var topMovableBar1 = sectionElement.children(":last");
                topMovableBar1.addClass("edoc2MoveAndResize");
                var setDefaultStyle;
                var setMovingStyle;
                if (sectionObject.type === 7)
                {
                    //箭头这个东西比较特殊。
                    setDefaultStyle = function ()
                    {
                        topMovableBar1.css("z-index", "100");
                        topMovableBar1.css("left", "50%");
                        topMovableBar1.css("width", "10px");
                        topMovableBar1.css("top", "50%");
                        topMovableBar1.css("height", "10px");
                        topMovableBar1.css("margin", "-5px");
                    };
                    setMovingStyle = function ()
                    {
                        topMovableBar1.css("z-index", "101");
                        topMovableBar1.css("width", "80px");
                        topMovableBar1.css("height", "80px");
                        topMovableBar1.css("margin", "-40px");
                    };
                } else
                {
                    setDefaultStyle = function ()
                    {
                        topMovableBar1.css("z-index", "100");
                        topMovableBar1.css("left", "25%");
                        topMovableBar1.css("width", "50%");
                        topMovableBar1.css("top", "0");
                        topMovableBar1.css("height", "8px");
                        topMovableBar1.css("border", "");
                        topMovableBar1.css("margin", "-4px 0 -4px 0");
                    };
                    setMovingStyle = function ()
                    {
                        topMovableBar1.css("z-index", "101");
                        topMovableBar1.css("left", "0");
                        topMovableBar1.css("width", "100%");
                        topMovableBar1.css("border", "40px solid transparent");
                        topMovableBar1.css("margin", "-44px 0 0 -40px");
                    };
                }
                setDefaultStyle();
                var ismoving;
                var currentleft;
                var currenttop;
                var currentX1;
                var currentY1;
                topMovableBar1.mousedown(function (e)
                {
                    ismoving = true;
                    currentleft = sectionElement[0].offsetLeft;
                    currenttop = sectionElement[0].offsetTop;
                    currentX1 = e.clientX;
                    currentY1 = e.clientY;
                    setMovingStyle();
                    return false;
                });
                topMovableBar1.mousemove(function (e)
                {
                    if (ismoving)
                    {
                        var newLeft = currentleft + e.clientX - currentX1;
                        if (newLeft >= 1 && newLeft + sectionElement[0].offsetWidth + 5 <= pageView.annotationLayer.div.offsetWidth)
                        {
                            sectionObject.left = newLeft / pageView.annotationLayer.div.offsetWidth;
                            sectionElement.css("left", (newLeft / pageView.annotationLayer.div.offsetWidth) * 100 + "%");
                        }
                        var newTop = currenttop + e.clientY - currentY1;
                        if (newTop >= 1 && newTop + sectionElement[0].offsetHeight + 5 <= pageView.annotationLayer.div.offsetHeight)
                        {
                            sectionObject.top = newTop / pageView.annotationLayer.div.offsetHeight;
                            sectionElement.css("top", (newTop / pageView.annotationLayer.div.offsetHeight) * 100 + "%");
                        }
                    }
                    return false;
                });
                topMovableBar1.mouseleave(function (e)
                {
                    if (ismoving)
                    {
                        ismoving = false;
                        setDefaultStyle();
                        self.editAnnotationSizeAndLocation(sectionObject);
                        return false;
                    }
                    return true;
                });
                topMovableBar1.mouseup(function (e)
                {
                    if (ismoving)
                    {
                        ismoving = false;
                        setDefaultStyle();
                        self.editAnnotationSizeAndLocation(sectionObject);
                    }
                    return false;
                });
            },

            showMessageDialog: function (message, callBack, confirm)
            {
                var self = this;

                var jq = window.top.$ || window.$;
                if (confirm)
                {
                    jq.messager.confirm(lang.confirm, message, function (r)
                    {
                        if (r && callBack != undefined)
                        {
                            callBack();
                        }
                    });
                } else
                {
                    $.messager.alert(lang.alert, message, "warning", function ()
                    {
                        if (callBack != undefined)
                        {
                            callBack();
                        }
                    });
                }
            },

            getDisplayText: function (key)
            {
                var s = lang[key];
                return s ? s : key;
            },
        };
        return pdfAnnotationController;
    })();

export default PdfAnnotationController;